package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.bean.NeedAuthMchntInfoBean;
import cc.rengu.igas.bsps.facade.request.NeedAuthMchntListQueryRequest;
import cc.rengu.igas.bsps.facade.response.NeedAuthMchntListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 待认证商户列表信息查询
 * Created by 王成 on 2020/3/23
 */
public class NeedAuthMchntListQueryService extends BankManagerService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        NeedAuthMchntListQueryRequest needAuthMchntListQueryRequest = new NeedAuthMchntListQueryRequest();
        ConvertUtil.convertOutput(needAuthMchntListQueryRequest);
        return needAuthMchntListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        NeedAuthMchntListQueryRequest needAuthMchntListQueryRequest = (NeedAuthMchntListQueryRequest) request;
        NeedAuthMchntListQueryResponse needAuthMchntListQueryResponse = new NeedAuthMchntListQueryResponse();
        BeanUtil.beanCopy(needAuthMchntListQueryRequest, needAuthMchntListQueryResponse);
        String instId = needAuthMchntListQueryRequest.getHeader().getInstId();
        String userId = needAuthMchntListQueryRequest.getUserId();
        String managerId = needAuthMchntListQueryRequest.getManagerId();
        int pageIndex = needAuthMchntListQueryRequest.getPageIndex();
        int pageSize = needAuthMchntListQueryRequest.getPageSize();
        /* 参数合法性检查 */
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(managerId)) {
            rglog.error("接口上传参数不合法,userId:<{}>,managerId:<{}>", userId, managerId);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 根据拓展经理工号查询拓展经理拓展的商户信息 */
        BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);
        PageResult pageResult = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoNeedAuthByManagerId(instId, managerId, pageInfo);
        List<MchntBaseInfo> mchntBaseInfoList = new ArrayList<>();
        if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
            rglog.info("根据查询条件查询到0条数据!");
            needAuthMchntListQueryResponse.setTotalPage(0);
            needAuthMchntListQueryResponse.setTotalRows(0);
        } else {
            for (Object object : pageResult.getResult()) {
                /* 只返回商户绑定表中没有绑定记录的商户 */
                MchntBaseInfo mchntBaseInfo = (MchntBaseInfo) object;
                BspsMchntBindInfoMapper bspsMchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
                List<MchntBindInfo> mchntBindInfoList = bspsMchntBindInfoMapper.selectBspsMchntBindInfoByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
                if (null == mchntBindInfoList || mchntBindInfoList.isEmpty()) {
                    mchntBaseInfoList.add((MchntBaseInfo) object);
                }
            }
            /* 返回查询结果 */
            List<NeedAuthMchntInfoBean> needAuthMchntInfos = new ArrayList<>();
            mchntBaseInfoList.forEach(item -> convMchntApplyInfo(item, needAuthMchntInfos));
            needAuthMchntListQueryResponse.setTotalPage((int) Math.ceil(needAuthMchntInfos.size() / pageSize));
            needAuthMchntListQueryResponse.setTotalRows(needAuthMchntInfos.size());
            needAuthMchntListQueryResponse.setNeedAuthMchntInfoList(needAuthMchntInfos);
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(needAuthMchntListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        NeedAuthMchntListQueryResponse needAuthMchntListQueryResponse = (NeedAuthMchntListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(needAuthMchntListQueryResponse);
    }

    /**
     * 查询结果转换
     *
     * @param mchntBaseInfo      商户基本信息
     * @param needAuthMchntInfos 应答接口对象
     */
    private void convMchntApplyInfo(MchntBaseInfo mchntBaseInfo, List<NeedAuthMchntInfoBean> needAuthMchntInfos) {
        NeedAuthMchntInfoBean needAuthMchntInfo = new NeedAuthMchntInfoBean();
        BeanUtil.beanCopy(mchntBaseInfo, needAuthMchntInfo);
        needAuthMchntInfo.setContactPhone(mchntBaseInfo.getMchntContactPhone());
        needAuthMchntInfo.setContactMobile(mchntBaseInfo.getMchntContactMobile());
        needAuthMchntInfo.setContactName(mchntBaseInfo.getMchntContactName());
        needAuthMchntInfos.add(needAuthMchntInfo);
    }
}
